import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gameplay_flutter/m_chatroom/src/template/music/model/room_music_center_controller.dart';
import 'package:get/get.dart';

import '../../../../../lib_baseview/lib_baseview.dart';

class MusicItemWidget extends StatelessWidget {
  // final Music item;
  final bool showAvatar;
  final VoidCallback? onPlayTap;

  final centerController = Get.find<RoomMusicCenterController>();

  MusicItemWidget({
    super.key,
    // required this.item,
    this.showAvatar = true,
    this.onPlayTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        // if (!centerController.isMusicLoading(item)) {
        //   onPlayTap?.call();
        //   centerController.play(item);
        // }
      },
      child: Container(
        height: 55,
        alignment: Alignment.center,
        color: Colors.white,
        padding: const EdgeInsets.symmetric(horizontal: 22),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          children: [
            if (showAvatar) const Text('AvatarImage'),
            // AvatarImage(
            //     url: item.poster,
            //     width: 52,
            //     height: 52,
            //     borderRadius: 8,
            //     shape: BoxShape.rectangle,
            //     fit: BoxFit.cover),
            if (showAvatar) const SizedBox(width: 12),
            const Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Flexible(
                      child: Text('item.name',
                          maxLines: 1,
                          style: TextStyle(
                              color: Color(0xFF333333),
                              fontSize: 13,
                              fontWeight: FontWeight.w500))),
                  SizedBox(height: 3),
                  Text('item.singer',
                      maxLines: 1,
                      style: TextStyle(color: Color(0xFF666666), fontSize: 11))
                ],
              ),
            ),
            const SizedBox(width: 12),
            GetBuilder<RoomMusicCenterController>(
                id: UpdateIds.collect,
                builder: (logic) {
                  // bool collected = logic.isCollected(item);
                  return GestureDetector(
                      onTap: () {
                        // if (collected) {
                        //   logic.delFromCollectSongCode(item.songCode ?? 0);
                        // } else {
                        //   logic.addToCollect(item);
                        // }
                      },
                      child: image(
                          // logic.isCollected(item)
                          //     ? 'ic_like.svg'
                          // :
                          'ic_unlike.svg',
                          width: 16,
                          fit: BoxFit.fitWidth));
                }),
            const SizedBox(width: 18),
            Container(
              width: 36,
              alignment: Alignment.centerRight,
              child: GetBuilder<RoomMusicCenterController>(
                id: UpdateIds.playState,
                builder: (logic) {
                  // if (logic.isMusicLoading(item)) {
                  //   return const SizedBox(
                  //       width: 20,
                  //       height: 20,
                  //       child: CupertinoActivityIndicator());
                  // }

                  // if (!logic.isPreloaded(item)) {
                  //   return const Icon(Icons.cloud_download_outlined,
                  //       size: 19, color: Color(0xFF999999));
                  // }

                  return Text('FutureBuilder');

                  // return FutureBuilder<MediaPlayerState>(
                  //   future: logic.playState(item),
                  //   builder: (c, shot) {
                  //     if (shot.hasData) {
                  //       MediaPlayerState state = shot.data!;
                  //       if (state == MediaPlayerState.playerStatePlaying) {
                  //         return Text(T.current!.song_playing,
                  //             style: const TextStyle(
                  //                 color: Color(0xFF999999), fontSize: 9));
                  //       } else {
                  //         return image('ic_music_play.svg',
                  //             width: 19, fit: BoxFit.fitWidth);
                  //       }
                  //     } else {
                  //       return image('ic_music_play.svg',
                  //           width: 19, fit: BoxFit.fitWidth);
                  //     }
                  //   },
                  // );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
